package com.team407.cheyoubang.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.team407.cheyoubang.R;
import com.team407.cheyoubang.listener.AcitionListener;
import com.team407.cheyoubang.logic.TabContent.TabValue;
/**
 * 主tab界面
 * @author Administrator
 *
 */

public class MainTabActivity extends TabActivity {

	public TabHost m_tabHost;
	private LayoutInflater mLayoutInflater;
	public static MainTabActivity mainTabActivity = null;
	private AcitionListener acitionListener[] = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_tab_layout);
		mainTabActivity = this;
		acitionListener = new AcitionListener[3];
		m_tabHost = getTabHost();
		mLayoutInflater = LayoutInflater.from(this);
		int count = TabValue.mTabClassArray.length;
		for (int i = 0; i < count; i++) {
			TabSpec tabSpec = m_tabHost.newTabSpec(TabValue.mTextviewArray[i])
					.setIndicator(getTabItemView(i))
					.setContent(getTabItemIntent(i));
			m_tabHost.addTab(tabSpec);
		}
		m_tabHost.setCurrentTab(0);
		m_tabHost.getTabWidget().getChildTabViewAt(0)
				.setOnTouchListener(new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN:
							break;
						case MotionEvent.ACTION_UP:
							if (acitionListener[0] != null) {
								acitionListener[0].actionInvoke();
							}
							break;
						}
						return false;
					}
				});
		m_tabHost.getTabWidget().getChildTabViewAt(1)
				.setOnTouchListener(new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN:
							break;
						case MotionEvent.ACTION_UP:
							if (acitionListener[1] != null) {
								acitionListener[1].actionInvoke();
							}
							break;
						}
						return false;
					}
				});
		m_tabHost.getTabWidget().getChildTabViewAt(2)
				.setOnTouchListener(new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN:
							break;
						case MotionEvent.ACTION_UP:
							if (acitionListener[2] != null) {
								acitionListener[2].actionInvoke();
							}
							break;
						}
						return false;
					}
				});

	}

	/***
	 * 设置点击监听器 tabPostion 标签的位置，从1开始
	 * */
	public void setActionTabListener(int tabPostion,
			AcitionListener acitionListener) {
		this.acitionListener[tabPostion - 1] = acitionListener;
	}

	private View getTabItemView(int index) {

		View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		if (imageView != null) {
			imageView
					.setBackgroundResource(TabValue.mTabBackagroundArray[index]);
		}
		return view;
	}

	private Intent getTabItemIntent(int index) {
		Intent intent = new Intent(this, TabValue.mTabClassArray[index]);
		return intent;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	// @Override
	// public boolean dispatchKeyEvent(KeyEvent event) {
	//
	// if(m_tabHost.getCurrentTab()==1)
	// {
	// if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() ==
	// KeyEvent.ACTION_DOWN)
	// {

	// }
	// }
	// else
	// {
	// m_tabHost.setCurrentTab(1);
	// return true;
	// }
	// return super.dispatchKeyEvent(event);
	// }

}
